﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DAO;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant
{
    public partial class frmThemBan : Form
    {
        KhuVucService kvService = new KhuVucService();
        BanService bService = new BanService();
        NhaHangService nhService = new NhaHangService();

        private Ban b = new Ban();
        public int flag = 0;

        public Ban B
        {
            get { return b; }
            set { b = value; }
        }

        public frmThemBan()
        {
            InitializeComponent();
        }

        public void LoadKhuVuc()
        {
            DataTable dbtb = kvService.LoadKhuVuc();
            for (int i = 0; i < dbtb.Rows.Count; i++)
            {
                DataRow dbrow = dbtb.Rows[i];
                cbbKhuVuc.Items.Add(dbrow["TenKhu"]);
            }
            cbbKhuVuc.Text = cbbKhuVuc.Items[0].ToString();
        }

        public void LoadTinhTrang()
        {
            cbbTinhTrang.Items.Add("Đã Đặt");
            cbbTinhTrang.Items.Add("Trống");
            cbbTinhTrang.Items.Add("Đang Sử Dụng");
            cbbTinhTrang.Text = cbbTinhTrang.Items[0].ToString();

        }

        private void LoadNH()
        {
            DataTable dbtab = nhService.LoadNH();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                cbbNH.Items.Add(dbrow["TenNH"]);
            }
            cbbNH.Text = cbbNH.Items[0].ToString();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Close();
        }

        private List<KhuVuc> listKhuVuc = new List<KhuVuc>();

        private void LoadKhuTheoNH(int ma)
        {
            cbbKhuVuc.Items.Clear();
            DataTable dbtab = kvService.TimKVMaNH(ma);
            if (dbtab.Rows.Count > 0)
            {
                for (int i = 0; i < dbtab.Rows.Count; i++)
                {
                    KhuVuc kv = new KhuVuc();
                    DataRow dbrow = dbtab.Rows[i];
                    kv.MaKhu = int.Parse(dbrow["MaKhu"].ToString());
                    kv.MaNH = int.Parse(dbrow["MaNH"].ToString());
                    kv.TenKhu = dbrow["TenKhu"].ToString();
                    listKhuVuc.Add(kv);
                    cbbKhuVuc.Items.Add(kv.TenKhu);
                }
                cbbKhuVuc.Text = cbbKhuVuc.Items[0].ToString();
            }
            else
            {
                cbbKhuVuc.Text = "";
            }
        }

        private void frmThemBan_Load(object sender, EventArgs e)
        {
            LoadNH();
            int maNH = nhService.LayMaNH(cbbNH.Text);
            LoadKhuTheoNH(maNH);
            LoadTinhTrang();
            LoadLoaiBan();
            if (flag == 1)
            {
                txtTenBan.Text = b.TenBan;
                KhuVuc kv = kvService.TimKVID(b.MaKhu);
                cbbKhuVuc.Text = kv.TenKhu;
                if (b.TinhTrang == 0)
                    cbbTinhTrang.Text = "Trống";
                if(b.TinhTrang == 1)
                    cbbTinhTrang.Text = "Đã Đặt";
                if (b.TinhTrang == 2)
                    cbbTinhTrang.Text = "Đang Sử Dụng";
                int maNH2 = nhService.LayMaNHTheoMaKhu(b.MaKhu);
                cbbNH.Text = nhService.LayTenNH(maNH2);
                if (b.MaLoaiBan == 1)
                    cbbLoaiBan.Text = "VIP";
                if (b.MaLoaiBan == 2)
                    cbbLoaiBan.Text = "Thường";
            }
        }
        public bool fix;
        private int LayMaKhu(int maNH)
        {
            string tenkhu = cbbKhuVuc.Text;
            for (int i = 0; i < listKhuVuc.Count; i++)
            {
                if (tenkhu == listKhuVuc[i].TenKhu && listKhuVuc[i].MaNH == maNH)
                    return listKhuVuc[i].MaKhu;
            }
            return -1;
        }

        private void LoadLoaiBan()
        {
            DataTable dbtab = bService.LoadLoaiBan();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                cbbLoaiBan.Items.Add(dbrow["TenLoaiBan"]);
            }
            cbbLoaiBan.Text = cbbLoaiBan.Items[0].ToString();
        }

        private int LayMaTinhTrang()
        {
            int tinhtrang = 0;
            if (cbbTinhTrang.Text == "Trống")
                tinhtrang = 0;
            if (cbbTinhTrang.Text == "Đã Đặt")
                tinhtrang = 1;
            if (cbbTinhTrang.Text == "Đang Sử Dụng")
                tinhtrang = 3;
            return tinhtrang;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                Ban b = new Ban();
                b.TenBan = txtTenBan.Text;
                int maNH = nhService.LayMaNH(cbbNH.Text);
                b.MaKhu = LayMaKhu(maNH);
                b.TinhTrang = LayMaTinhTrang();
                b.MaLoaiBan = bService.LayMaLoaiBan(cbbLoaiBan.Text);
                if (bService.ThemBan(b))
                {
                    MessageBox.Show("Thành Công");
                    this.Close();
                    return;
                }
                MessageBox.Show("Thêm bàn thất bai !", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
               
            else if (flag == 1)
            {
                flag = 0;
                int maNH = nhService.LayMaNH(cbbNH.Text);
                b.TenBan = txtTenBan.Text;
                b.MaKhu = LayMaKhu(maNH);
                b.TinhTrang = LayMaTinhTrang();
                bool kq;
                if (fix)
                    kq = bService.CapNhapBanDirtyread(b);
                else
                    kq = bService.CapNhapBanDirtyreadError(b);

                if (kq)
                {
                    MessageBox.Show("Thành Công");
                    this.Close();
                    return;
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    return;
                }
            }
        }

        private void cbbNH_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int maNH = nhService.LayMaNH(cbbNH.Text);
                LoadKhuTheoNH(maNH);
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
